<?php


namespace App\Service;


use App\Exceptions\ApiException;
use App\Model\ResourceModel;
use EasySwoole\ORM\Exception\Exception;

class ResourceService
{

    public static function savaResource($data)
    {
        try {

            $resource = ResourceModel::create(
                [
                    'resource_name' => $data['name'],
                    'md5' => $data['md5'],
                    'sha1' => $data['sha1'],
                    'path' => $data['path'],
                    'status' => 1,
                    'storage' => 'qiniu',
                    'create_time' => time(),
                    'user_id' => $data['user_id'],
                    'filesize' => $data['filesize'],
                    'ext' => $data['ext'],
                ]
            );
            $resource_id = $resource->save();
            return $resource_id;

        } catch (Exception $e) {
            throw new ApiException($e->getMessage());
        }

    }

    public static function getResourceByMd5($md5)
    {
        $res = ResourceModel::create()->field('id,path,resource_name as name,storage')->where(['md5' => $md5, 'status' => 1])->get();
        return $res;
    }

}